package com.ogawa.a7517.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.View;
import android.view.Window;

import androidx.annotation.NonNull;

import com.ogawa.a7517.Constant;
import com.ogawa.a7517.R;
import com.ogawa.a7517.activity.MainActivity7517;

import java.lang.ref.SoftReference;

public class TimeDialog extends Dialog implements View.OnClickListener {
    private int value;
    private NumberPickerView npvTime;
    private SoftReference<MainActivity7517> activity;

    public TimeDialog(@NonNull Context context) {
        super(context);
        if (context instanceof MainActivity7517) {
            activity = new SoftReference<>((MainActivity7517) context);
        }
        getWindow().addFlags(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.dialog_time_select);
        findViewById(R.id.iv_close).setOnClickListener(this);
        npvTime = findViewById(R.id.npv_time_select);
        npvTime.setOnValueChangedListener((picker, oldValue, newValue) -> value = newValue);
        findViewById(R.id.iv_confirm).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_close) {
            dismiss();
        } else if (id == R.id.iv_confirm) {
            writeTimeCommand();
            dismiss();
        }
    }

    private void writeTimeCommand() {
        if (activity == null) {
            return;
        }
        String time = "";
        switch (value) {
            case 0:
                time = "5";
                break;
            case 1:
                time = "10";
                break;
            case 2:
                time = "15";
                break;
            case 3:
                time = "20";
                break;
            case 4:
                time = "25";
                break;
            case 5:
                time = "30";
                break;
        }
        Log.e("tag", "-------------time------------" + time);
        activity.get().publish(Constant.TIME_ADJUST, "3", time);
    }

    @Override
    public void show() {
        value = 2;
        npvTime.setPickedIndexRelativeToMin(2);
        super.show();
    }
}
